package com.zw.yzk.kabu.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseApplication;
import com.zw.yzk.kabu.internal.component.DaggerWXEntityComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.WechatLoginModule;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.presenter.WecahtLoginPresenter;
import com.zw.yzk.kabu.view.activity.MainActivity;
import com.zw.yzk.kabu.view.view.LoginView;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/2/28.
 */

public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler, LoginView {

    private IWXAPI api;

    @Inject
    WecahtLoginPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initComponent();
        api = WXAPIFactory.createWXAPI(this, WechatManager.WECHAT_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initComponent();
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destroy();
    }

    @Override
    public void onReq(BaseReq resp) {
        //sendReq是第三方app主动发送消息给微信，发送完成之后会切回到第三方app界面。
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        //sendResp是微信向第三方app请求数据，第三方app回应数据之后会切回到微信界面。
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                //发送成功
                String code = ((SendAuth.Resp) resp).code;
                String state = ((SendAuth.Resp) resp).state;
                login(code, state);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                ToastManager.getInstance().showToast(this, "发送取消");
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                ToastManager.getInstance().showToast(this, "发送被拒绝");
                finish();
                break;
            default:
                ToastManager.getInstance().showToast(this, "发送返回");
                finish();
                break;
        }
    }

    @Override
    public void loginSuccess() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void loginFail() {
        finish();
        ToastManager.getInstance().showToast(this, getResources().getString(R.string.login_fail));
    }

    private void initComponent() {
        DaggerWXEntityComponent.builder()
                .applicationComponent(BaseApplication.getInstance().getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .wechatLoginModule(new WechatLoginModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    /**
     * 登录
     *
     * @param code  微信返回code
     * @param state 微信返回
     */
    private void login(String code, String state) {
        if (presenter == null) {
            initComponent();
        }
        presenter.login(code, state);
    }
}
